//
//  GameManager.cpp
//  DiceWar
//
//  Created by Xiao  Feng on 16/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include "SceneManager.h"
#include "MainmenuLayer.h"
#include "MapSelectionLayer.h"
#include "WarSettingLayer.h"
#include "WarLayer.h"

SceneManager::SceneManager() {}
SceneManager::~SceneManager() {}

SceneManager& SceneManager::sharedSceneManager()
{
    static SceneManager instance;
    return instance;
}

bool SceneManager::init()
{
    registerScene(kMainMenuScene, createSceneWithLayer<MainmenuLayer>());
    registerScene(kMapSelectionScene, createSceneWithLayer<MapSelectionLayer>());
    registerScene(kGameScene, createSceneWithLayer<WarLayer>());
    registerScene(kWarSettingScene, createSceneWithLayer<WarSettingLayer>());
    
    startWithScene(kGameScene);
    return false;
}

void SceneManager::registerScene(SceneManager::SceneType type, cocos2d::CCScene *pScene)
{
    CCAssert(pScene, "The Scene is NULL!");
    if (!pScene) return;
    
    m_sceneMap.insert(std::make_pair(type, pScene));
}

void SceneManager::switchToScene(SceneType scene)
{
    cocos2d::CCScene* pScene = getScene(scene);
    cocos2d::CCDirector::sharedDirector()->pushScene(pScene);
}

void SceneManager::startWithScene(SceneType scene)
{
    cocos2d::CCScene* pScene = getScene(scene);
    cocos2d::CCDirector::sharedDirector()->runWithScene(pScene);
}

cocos2d::CCScene* SceneManager::getScene(SceneType scene)
{
    SceneMap::const_iterator iter = m_sceneMap.find(scene);
    if (iter == m_sceneMap.end()) return NULL;
    
    return iter->second;
}


